package com.blog.sys.controller;

import com.blog.blog.controller.CommonController;
import com.blog.sys.dao.SysRoleRepository;
import com.blog.sys.entity.SysRole;
import com.blog.common.newclass.Result;
import com.blog.common.newclass.Selector;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class SysRoleController extends CommonController {
    private final static Logger logger = LoggerFactory.getLogger(SysRoleController.class);

    @Autowired
    private SysRoleRepository sysRoleRepository;

    /**
     * 触发MyShiroRealm.doGetAuthenticationInfo()实现登录
     *
     * @param request
     * @param mv
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    @RequiresPermissions("roleInfo:view")
    public ModelAndView list(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/roleInfo");
        return mv;
    }

    /**
     * 触发MyShiroRealm.doGetAuthenticationInfo()实现登录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @RequiresPermissions("roleInfo:view")
    public Result query(SysRole sysRole) {
        List<SysRole> all = sysRoleRepository.findAll();
        long count = sysRoleRepository.count();
        return page(all, count);
    }


    @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @RequiresPermissions("roleInfo:add")
    public Result add(SysRole role, ModelAndView mv) {
        try {
            sysRoleRepository.save(role);
        } catch (RuntimeException e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            logger.error(sw.toString());
            return error(sqlErrMsg(e));
        }
        return success("保存成功", role);
    }

    @RequestMapping(value = "/findByRoleName", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @RequiresPermissions("roleInfo:view")
    public Result findByRole() {
            List<Selector> selectors = new ArrayList<>();
        try {
            List<SysRole> byRole = sysRoleRepository.findAll();
            for (SysRole ro : byRole) {
                selectors.add(new Selector(ro.getDescription(),String.valueOf(ro.getId())));
            }
        } catch (RuntimeException e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            logger.error(sw.toString());
            return error(sqlErrMsg(e));
        }
        return success("查询成功", selectors);
    }

    @RequestMapping("/del")
    @RequiresPermissions("roleInfo:del")
    public ModelAndView del(HttpServletRequest request, Map<String, Object> map, ModelAndView mv) {
        mv.setViewName("/roleInfoDel");
        mv.addObject(RES, success("", null));
        return mv;
    }
}